using Microsoft.NetEnterpriseServers;
using Microsoft.SqlServer.Management.SqlManagerUI.Scripting;
using Microsoft.SqlServer.Management.SqlMgmt;
using Microsoft.SqlServer.Management.UI;
using Microsoft.SqlServer.Tools.HostingServices;
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Net;
using System.Windows.Forms;

namespace Microsoft.SqlServer.Management.SqlManagerUI
{
	public class ProviderListDialog : Form
	{
		private ProviderEntity _selectedProvider;

		private ProviderCollection _providers;

		private ProviderDetailDialog _providerDetail;

		private IMessageBoxProvider _messageProvider;

		private WizardPage _parent;

		private IContainer components;

		private Label lblConfiguredProvider;

		private Panel pnlButtonsPanel;

		private Panel pnlFooterButtons;

		private Button btnTest;

		private Button btnDelete;

		private Button btnEdit;

		private Button btnNew;

		private Button btnOk;

		private Button btnCancel;

		private DataGridViewTextBoxColumn clmWebServiceAddress;

		private DataGridViewTextBoxColumn clmProviderName;

		private DataGridView dgvProviderList;

		public WizardPage WizardParent
		{
			get
			{
				return this._parent;
			}
		}

		public ReadOnlyCollection<ProviderEntity> Providers
		{
			get
			{
				return new ReadOnlyCollection<ProviderEntity>(this._providers);
			}
		}

		public ProviderEntity SelectedProvider
		{
			get
			{
				this._selectedProvider = null;
				if (this.dgvProviderList.SelectedCells != null)
				{
					int num = -1;
					if (this.dgvProviderList.SelectedCells.Count <= 0)
					{
						if (this.dgvProviderList.Rows.Count > 0)
						{
							this.dgvProviderList.Rows[0].Selected = true;
							num = 0;
						}
					}
					else
					{
						num = this.dgvProviderList.SelectedCells[0].RowIndex;
					}
					if (num >= 0)
					{
						this._selectedProvider = this._providers[num];
					}
				}
				return this._selectedProvider;
			}
		}

		private ProviderDetailDialog ProviderDetailDialog
		{
			get
			{
				if (this._providerDetail == null)
				{
					this._providerDetail = new ProviderDetailDialog(this.WizardParent, this._messageProvider);
				}
				return this._providerDetail;
			}
		}

		public ProviderListDialog(WizardPage parent, IMessageBoxProvider messageProvider, ProviderCollection providers)
		{
			this.InitializeComponent();
			this.Init();
			this._providers = providers;
			if (this._providers != null)
			{
				this.BindProvidersDataGrid();
			}
			this._parent = parent;
			this._messageProvider = messageProvider;
			base.Icon = CUtils.LoadIcon("server.ico");
		}

		private void LoadStrings()
		{
			this.lblConfiguredProvider.Text = GenScriptWizSR.ConfiguredProviders;
			this.Text = GenScriptWizSR.ProviderListTitle;
			this.btnNew.Text = GenScriptWizSR.NewText;
			this.btnEdit.Text = GenScriptWizSR.EditText;
			this.btnDelete.Text = GenScriptWizSR.DeleteText;
			this.btnTest.Text = GenScriptWizSR.TestText;
			this.btnOk.Text = GenScriptWizSR.OK;
			this.btnCancel.Text = GenScriptWizSR.CancelBtn;
			this.clmProviderName.HeaderText = GenScriptWizSR.ProviderName;
			this.clmWebServiceAddress.HeaderText = GenScriptWizSR.WebServiceAddress;
		}

		private void Init()
		{
			this.LoadStrings();
		}

		private void BindProvidersDataGrid()
		{
			this.dgvProviderList.Rows.Clear();
			this.dgvProviderList.AutoGenerateColumns = true;
			this._providers.Sort(new ProvidersComparer());
			foreach (ProviderEntity current in this._providers)
			{
				this.dgvProviderList.Rows.Add(new string[]
				{
					current.Name,
					current.WebServiceAddress
				});
			}
		}

		private void CloseDialog()
		{
			base.Close();
		}

		private bool EditProvider(ProviderEntity provider)
		{
			this.ProviderDetailDialog.Provider = provider;
			this.ProviderDetailDialog.ShowDialog(this);
			return this.ProviderDetailDialog.IsOkSelected;
		}

		private static bool DeleteProvider(ProviderEntity provider)
		{
			HostingConfigurationManager.DeleteProvider(provider);
			return true;
		}

		private bool TestProvider(ProviderEntity provider)
		{
			string password = provider.Password;
			bool result;
			try
			{
				if (string.IsNullOrEmpty(provider.Password) && new RemoteCreditDialog
				{
					Provider = provider,
					Database = null
				}.ShowDialog(this) == DialogResult.Cancel)
				{
					result = false;
				}
				else
				{
					new PublishService
					{
						Url = provider.WebServiceAddress,
						Credentials = new NetworkCredential(provider.UserName, provider.Password),
						CookieContainer = new CookieContainer()
					}.GetServiceOptions();
					this._messageProvider.ShowMessage(string.Format(CultureInfo.CurrentCulture, GenScriptWizSR.TestSuccessMessage, new object[]
					{
						provider.Name
					}), GenScriptWizSR.TestTitleSuccess, ExceptionMessageBoxButtons.OK, ExceptionMessageBoxSymbol.Information, this.WizardParent);
					result = true;
				}
			}
			catch (Exception ex)
			{
				GenScriptWizForm.ShowErrorMessageForProvider(this._messageProvider, provider, ex, this.WizardParent);
				result = false;
			}
			finally
			{
				provider.Password = password;
			}
			return result;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			this.CloseDialog();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.CloseDialog();
		}

		private void btnNew_Click(object sender, EventArgs e)
		{
			ProviderEntity providerEntity = new ProviderEntity();
			if (this.EditProvider(providerEntity))
			{
				try
				{
					HostingConfigurationManager.AddProvider(providerEntity);
					this._providers.Add(providerEntity);
					this.BindProvidersDataGrid();
					int num = 0;
					foreach (DataGridViewRow dataGridViewRow in ((IEnumerable)this.dgvProviderList.Rows))
					{
						if (this._providers[num++] == providerEntity)
						{
							dataGridViewRow.Selected = true;
						}
						else if (dataGridViewRow.Selected)
						{
							dataGridViewRow.Selected = false;
						}
					}
				}
				catch (Exception e2)
				{
					this._messageProvider.ShowMessage(e2, string.Empty, ExceptionMessageBoxButtons.OK, ExceptionMessageBoxSymbol.Warning, this.WizardParent);
				}
			}
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			ProviderEntity selectedProvider = this.SelectedProvider;
			if (selectedProvider != null && this.EditProvider(selectedProvider))
			{
				try
				{
					HostingConfigurationManager.UpdateProvider(selectedProvider);
					this.dgvProviderList.Rows[this.dgvProviderList.SelectedCells[0].RowIndex].SetValues(new string[]
					{
						selectedProvider.Name,
						selectedProvider.WebServiceAddress
					});
				}
				catch (Exception e2)
				{
					this._messageProvider.ShowMessage(e2, GenScriptWizSR.ERROR_SaveFailedTitle, ExceptionMessageBoxButtons.OK, ExceptionMessageBoxSymbol.Warning, this.WizardParent);
				}
			}
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			ProviderEntity selectedProvider = this.SelectedProvider;
			if (selectedProvider != null)
			{
				DialogResult dialogResult = this._messageProvider.ShowMessage(GenScriptWizSR.ProviderDeleteMessage, GenScriptWizSR.ProviderDeleteTitle, ExceptionMessageBoxButtons.YesNoCancel, ExceptionMessageBoxSymbol.Question, this.WizardParent);
				if (dialogResult == DialogResult.Yes)
				{
					try
					{
						if (ProviderListDialog.DeleteProvider(selectedProvider))
						{
							this._providers.Remove(selectedProvider);
							this.BindProvidersDataGrid();
						}
					}
					catch (Exception e2)
					{
						this._messageProvider.ShowMessage(e2, GenScriptWizSR.ERROR_DeleteFailedTitle, ExceptionMessageBoxButtons.OK, ExceptionMessageBoxSymbol.Warning, this.WizardParent);
					}
				}
			}
		}

		private void btnTest_Click(object sender, EventArgs e)
		{
			ProviderEntity selectedProvider = this.SelectedProvider;
			if (selectedProvider != null)
			{
				this.TestProvider(selectedProvider);
			}
		}

		private void ProviderListDialog_Load(object sender, EventArgs e)
		{
			if (this._providers != null && this._providers.Count > 0)
			{
				this.dgvProviderList.Rows[0].Cells[0].Selected = false;
				this.dgvProviderList.Rows[0].Selected = true;
			}
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}

		private void InitializeComponent()
		{
			this.lblConfiguredProvider = new Label();
			this.pnlButtonsPanel = new Panel();
			this.btnTest = new Button();
			this.btnDelete = new Button();
			this.btnEdit = new Button();
			this.btnNew = new Button();
			this.pnlFooterButtons = new Panel();
			this.btnOk = new Button();
			this.btnCancel = new Button();
			this.clmWebServiceAddress = new DataGridViewTextBoxColumn();
			this.clmProviderName = new DataGridViewTextBoxColumn();
			this.dgvProviderList = new DataGridView();
			this.pnlButtonsPanel.SuspendLayout();
			this.pnlFooterButtons.SuspendLayout();
			((ISupportInitialize)this.dgvProviderList).BeginInit();
			base.SuspendLayout();
			this.lblConfiguredProvider.AutoSize = true;
			this.lblConfiguredProvider.Location = new Point(12, 9);
			this.lblConfiguredProvider.Name = "lblConfiguredProvider";
			this.lblConfiguredProvider.Size = new Size(114, 13);
			this.lblConfiguredProvider.TabIndex = 0;
			this.lblConfiguredProvider.Text = "A:Configured providers";
			this.pnlButtonsPanel.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Right);
			this.pnlButtonsPanel.Controls.Add(this.btnTest);
			this.pnlButtonsPanel.Controls.Add(this.btnDelete);
			this.pnlButtonsPanel.Controls.Add(this.btnEdit);
			this.pnlButtonsPanel.Controls.Add(this.btnNew);
			this.pnlButtonsPanel.Location = new Point(464, 28);
			this.pnlButtonsPanel.Name = "pnlButtonsPanel";
			this.pnlButtonsPanel.Size = new Size(97, 196);
			this.pnlButtonsPanel.TabIndex = 1;
			this.btnTest.Location = new Point(2, 90);
			this.btnTest.Name = "btnTest";
			this.btnTest.Size = new Size(91, 23);
			this.btnTest.TabIndex = 4;
			this.btnTest.Text = "A:Test";
			this.btnTest.UseVisualStyleBackColor = true;
			this.btnTest.Click += new EventHandler(this.btnTest_Click);
			this.btnDelete.Location = new Point(2, 61);
			this.btnDelete.Name = "btnDelete";
			this.btnDelete.Size = new Size(91, 23);
			this.btnDelete.TabIndex = 3;
			this.btnDelete.Text = "A:Delete";
			this.btnDelete.UseVisualStyleBackColor = true;
			this.btnDelete.Click += new EventHandler(this.btnDelete_Click);
			this.btnEdit.Location = new Point(3, 32);
			this.btnEdit.Name = "btnEdit";
			this.btnEdit.Size = new Size(91, 23);
			this.btnEdit.TabIndex = 2;
			this.btnEdit.Text = "A:Edit...";
			this.btnEdit.UseVisualStyleBackColor = true;
			this.btnEdit.Click += new EventHandler(this.btnEdit_Click);
			this.btnNew.Location = new Point(3, 3);
			this.btnNew.Name = "btnNew";
			this.btnNew.Size = new Size(91, 23);
			this.btnNew.TabIndex = 1;
			this.btnNew.Text = "A:New...";
			this.btnNew.UseVisualStyleBackColor = true;
			this.btnNew.Click += new EventHandler(this.btnNew_Click);
			this.pnlFooterButtons.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.pnlFooterButtons.Controls.Add(this.btnOk);
			this.pnlFooterButtons.Controls.Add(this.btnCancel);
			this.pnlFooterButtons.Location = new Point(15, 230);
			this.pnlFooterButtons.Name = "pnlFooterButtons";
			this.pnlFooterButtons.Size = new Size(546, 29);
			this.pnlFooterButtons.TabIndex = 2;
			this.btnOk.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
			this.btnOk.Location = new Point(383, 3);
			this.btnOk.Name = "btnOk";
			this.btnOk.Size = new Size(75, 23);
			this.btnOk.TabIndex = 5;
			this.btnOk.Text = "A:OK";
			this.btnOk.UseVisualStyleBackColor = true;
			this.btnOk.Click += new EventHandler(this.btnOk_Click);
			this.btnCancel.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);
			this.btnCancel.DialogResult = DialogResult.Cancel;
			this.btnCancel.Location = new Point(464, 3);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new Size(75, 23);
			this.btnCancel.TabIndex = 6;
			this.btnCancel.Text = "A:Cancel";
			this.btnCancel.UseVisualStyleBackColor = true;
			this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
			this.clmWebServiceAddress.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			this.clmWebServiceAddress.HeaderText = "A:Web service address";
			this.clmWebServiceAddress.Name = "clmWebServiceAddress";
			this.clmWebServiceAddress.ReadOnly = true;
			this.clmWebServiceAddress.SortMode = DataGridViewColumnSortMode.NotSortable;
			this.clmProviderName.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
			this.clmProviderName.HeaderText = "A:Name";
			this.clmProviderName.Name = "clmProviderName";
			this.clmProviderName.ReadOnly = true;
			this.clmProviderName.SortMode = DataGridViewColumnSortMode.NotSortable;
			this.dgvProviderList.AllowUserToAddRows = false;
			this.dgvProviderList.AllowUserToDeleteRows = false;
			this.dgvProviderList.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.dgvProviderList.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.dgvProviderList.Columns.AddRange(new DataGridViewColumn[]
			{
				this.clmProviderName,
				this.clmWebServiceAddress
			});
			this.dgvProviderList.Location = new Point(15, 28);
			this.dgvProviderList.MultiSelect = false;
			this.dgvProviderList.Name = "dgvProviderList";
			this.dgvProviderList.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
			this.dgvProviderList.Size = new Size(443, 196);
			this.dgvProviderList.TabIndex = 3;
			base.AcceptButton = this.btnOk;
			base.AutoScaleDimensions = new SizeF(6f, 13f);
			base.AutoScaleMode = AutoScaleMode.Font;
			base.CancelButton = this.btnCancel;
			base.ClientSize = new Size(566, 266);
			base.Controls.Add(this.dgvProviderList);
			base.Controls.Add(this.pnlFooterButtons);
			base.Controls.Add(this.pnlButtonsPanel);
			base.Controls.Add(this.lblConfiguredProvider);
			this.MinimumSize = new Size(574, 300);
			base.Name = "ProviderListDialog";
			base.ShowInTaskbar = false;
			base.StartPosition = FormStartPosition.CenterParent;
			this.Text = "A:Hosting Providers";
			base.Load += new EventHandler(this.ProviderListDialog_Load);
			this.pnlButtonsPanel.ResumeLayout(false);
			this.pnlFooterButtons.ResumeLayout(false);
			((ISupportInitialize)this.dgvProviderList).EndInit();
			base.ResumeLayout(false);
			base.PerformLayout();
		}
	}
}
